package com.hushao.lims.weblims.controller;

import com.hushao.lims.weblims.domain.EquipmentRepair;
import com.hushao.lims.weblims.domain.TeacherInfo;
import com.hushao.lims.weblims.service.EquipmentRepairService;
import com.hushao.lims.weblims.vo.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 930588706
 * @date 18/1/28
 * 器材保修
 */
@Controller
@RequestMapping("/equipmentRepair")
public class EquipmentRepairController {
    @Autowired
    private EquipmentRepairService equipmentRepairService;

    @Autowired
    private UserDetailsService userDetailsService;

    /**
     * 获取保修列表
     * @param equipmentName
     * @param state
     * @param async
     * @param pageable
     * @param model
     * @return
     * @throws Exception
     */
    @ApiOperation("获取报修列表（分页）")
    @PreAuthorize("hasAuthority('ROLE_主管')")
    @GetMapping
    public String listEquipmentRepertory(@RequestParam(value = "equipmentName",required = false,defaultValue = "") String equipmentName,
                                         @RequestParam(value = "state",required = false,defaultValue = "") String state,
                                         @RequestParam(value = "async",required = false) boolean async,
                                         Pageable pageable,
                                         Model model)throws Exception{
        Page<EquipmentRepair> equipmentRepairsPage= equipmentRepairService.listEquipmentRepairEquipmentNameLikeAndStateLike(equipmentName,state,pageable);
        List<EquipmentRepair> equipmentRepairs=equipmentRepairsPage.getContent();
        model.addAttribute("page",equipmentRepairsPage);
        model.addAttribute("equipmentRepairs",equipmentRepairs);
        return async==true?"/audit/equipmentRepairList ::#mainContainerRepleace":"/audit/equipmentRepairList";
    }

    /**
     * 获取个人报修列表
     * @param teacherNumber
     * @param equipmentName
     * @param async
     * @param pageable
     * @param model
     * @return
     * @throws Exception
     */
    @ApiOperation(value ="获取个人器材报修列表" )
    @GetMapping("/{teacherNumber}")
    public String listEquipmentRepertoryByTeacher(@PathVariable("teacherNumber")String teacherNumber,
                                                  @RequestParam(value = "equipmentName",required = false,defaultValue = "") String equipmentName,
                                                  @RequestParam(value = "async",required = false) boolean async,
                                                  Pageable pageable,
                                                  Model model)throws Exception{
        TeacherInfo teacher= (TeacherInfo) userDetailsService.loadUserByUsername(teacherNumber);
        Page<EquipmentRepair> equipmentRepairsPage= equipmentRepairService.listEquipmentRepertoryByTeacherAndEquipmentName(equipmentName,teacher,pageable);
        List<EquipmentRepair> equipmentRepairs=equipmentRepairsPage.getContent();
        model.addAttribute("page",equipmentRepairsPage);
        model.addAttribute("equipmentRepairs",equipmentRepairs);
        return async==true?"/equipments/repairsList ::#mainContainerRepleace":"/equipments/repairsList";
    }

    /**
     * 撤销器材报修(个人)
     * @param repairId
     * @param teacherNumber
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "撤销器材报修(个人)")
    @ResponseBody
    @DeleteMapping("/{teacherNumber}/revocation/{repairId}")
    @PreAuthorize("authentication.name.equals(#teacherNumber)")
    public ResponseData revocation(@PathVariable("repairId") int repairId,
                                   @PathVariable("teacherNumber") String teacherNumber)throws Exception{
        equipmentRepairService.remove(repairId);
        return new ResponseData(true,"撤销成功！");
    }

    /**
     * 获取器材报修完成界面
     * @param repairId
     * @param teacherNumber
     * @param model
     * @return
     */
    @ApiOperation(value ="获取器材报修完成界面" )
    @GetMapping("/{teacherNumber}/repairOk/{repairId}")
    @PreAuthorize("authentication.name.equals(#teacherNumber)")
    public String repairOkInterface(@PathVariable("repairId") int repairId,
                                   @PathVariable("teacherNumber") String teacherNumber,
                                   Model model){
        EquipmentRepair equipmentRepair=equipmentRepairService.findOne(repairId);
        model.addAttribute("equipmentRepair",equipmentRepair);
        return "equipments/repairOk";
    }


    /**
     * 完成器材报修
     * @param repairId
     * @param teacherNumber
     * @param repairPrice
     * @return
     * @throws Exception
     */
    @ApiOperation(value ="完成器材报废服务" )
    @PreAuthorize("hasAuthority('ROLE_实验室管理员') and authentication.name.equals(#teacherNumber)")
    @PostMapping
    @ResponseBody
    public ResponseData repairsOK(int repairId,
                                  String teacherNumber,
                                  BigDecimal repairPrice) throws Exception{
        equipmentRepairService.achieve(repairId,repairPrice);
        return new ResponseData(true,"器材报修完成！");
    }
}
